//
//  EditExpressVC.m
//  LazyCat
//
//  Created by linhui on 16/1/5.
//  Copyright (c) 2016年 zhanshu. All rights reserved.
//

#import "EditExpressVC.h"
#import "AddExpressCell.h"
//#import "ZBarViewController.h"

@interface EditExpressVC ()<UITableViewDataSource,UITableViewDelegate,UITextFieldDelegate>
{
    UITableView *_tableView;
    NSMutableArray *_titleArr;
    NSArray *_placeHodler;
}
@end

@implementation EditExpressVC

- (void)viewDidLoad {
    [super viewDidLoad];
    [self createUI];
}
- (void)createUI {
    [self initNav:@"编辑快递" doBack:YES];
    UIView *lineView = [[UIView alloc]initWithFrame:CGRectMake(0, 63, ScreenWidth, 1)];
    lineView.backgroundColor = [UIColor lightGrayColor];
    lineView.alpha = 0.2;
    [self.navBar addSubview:lineView];
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 64, ScreenWidth, ScreenHeight - 64) style:UITableViewStyleGrouped];
    _tableView.scrollEnabled = NO;
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [self.view addSubview:_tableView];
    UIButton *addBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    addBtn.backgroundColor = MainColor;
    addBtn.titleLabel.font = [UIFont systemFontOfSize:16];
    [addBtn setTitle:@"保存" forState:UIControlStateNormal];
    [addBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    addBtn.tag = 1001;
    [addBtn addTarget:self action:@selector(rightBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    addBtn.frame = CGRectMake((ScreenWidth-250)/2, 504, 250, 40);
    addBtn.layer.cornerRadius = 5.0;
    addBtn.layer.masksToBounds = YES;
    [self.view addSubview:addBtn];
    _titleArr = [NSMutableArray array];
    NSArray *titleArr = @[@"快递单号",@"快递公司",@"快递代送费",@"存放货柜号",@"手机号码",@"备注"];
    NSArray *titleArrr = @[@"是否代送",@"是否已经被领取"];
    [_titleArr addObject:titleArr];
    [_titleArr addObject:titleArrr];
//    _placeHodler = @[@"请输入快递单号",@"请输入快递公司",@"请输入手机号码",@"请输入货柜号",@""];
}
- (void)rightBtnClick:(UIButton *)sender {
    if (sender.tag == 1001) {
        NSMutableArray *arr = [[NSMutableArray alloc]init];
        UITextField *textfield1 = (UITextField *)[self.view viewWithTag:100];
        [arr addObject:textfield1.text];
        UITextField *textfield2 = (UITextField *)[self.view viewWithTag:101];
        [arr addObject:textfield2.text];
        UITextField *textfield3 = (UITextField *)[self.view viewWithTag:102];
        [arr addObject:textfield3.text];
        UITextField *textfield4 = (UITextField *)[self.view viewWithTag:103];
        [arr addObject:textfield4.text];
        UITextField *textfield5 = (UITextField *)[self.view viewWithTag:104];
        [arr addObject:textfield5.text];
        [self.navigationController popViewControllerAnimated:YES];
    }else if (sender.tag == 1002){
        // 返回按钮
//        ZBarViewController *zbar = [[ZBarViewController alloc] init];
//        [self.navigationController pushViewController:zbar animated:YES];
    }
}
#pragma mark -- tableView
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    AddExpressCell *cell = [tableView dequeueReusableCellWithIdentifier:@"addcell"];
    if (cell == nil) {
        cell = [[NSBundle mainBundle]loadNibNamed:@"AddExpressCell" owner:nil options:nil][0];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    if (indexPath.section == 0 && indexPath.row == 0) {
        cell.textField.text = _orderNum;
        cell.rightBtn.hidden = NO;
        cell.clickBtn.hidden = NO;
        cell.clickBtn.tag = 1002;
        [cell.clickBtn addTarget:self action:@selector(rightBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    }else if (indexPath.section == 0 && indexPath.row == 1){
        cell.textField.text = _EXcompany;
    }else if (indexPath.section == 0 && indexPath.row == 2){
        cell.textField.text = _EXmoney;
    }else if (indexPath.section == 0 && indexPath.row == 3){
        cell.textField.text = _EXnum;
    }else if (indexPath.section == 0 && indexPath.row == 4){
        cell.textField.text = _EXphoneMum;
    }else if (indexPath.section == 0 && indexPath.row == 5){
        cell.textField.text = _EXbeizhu;
    }else if (indexPath.section == 1){
        cell.textField.hidden = YES;
        UISwitch *changeSwitch = [[UISwitch alloc]initWithFrame:CGRectMake(0, 0, 0, 0)];
        changeSwitch.onTintColor=  MainColor;
        [changeSwitch addTarget:self action:@selector(switchChangedValue:) forControlEvents:UIControlEventValueChanged];
        changeSwitch.transform = CGAffineTransformMakeScale(0.9, .9);
        changeSwitch.tag = 10+indexPath.row;
        cell.accessoryView = changeSwitch;
    }
    cell.titleLabel.text = _titleArr[indexPath.section][indexPath.row];
    cell.textField.delegate = self;
    cell.textField.tag = 100+indexPath.row;
    return cell;
}
- (void)switchChangedValue:(UISwitch *)sender {
    if (sender.tag == 10) {//是否代送
        NSLog(@"%d",sender.isOn);
    }else if (sender.tag == 11){//是否已经被领取
        NSLog(@"%d",sender.isOn);
    }
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return _titleArr.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [_titleArr[section] count];
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 50;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 0.0001;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 20;
}
@end